package com.ymatou.app.rec.testcase;


import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.app.rec.parameter.HotBean;
import com.ymatou.app.rec.service.HotCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
/**
*热搜词提示接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Hot{
	private static HotBean hotBean;
	private static HotCall hotCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("热搜词提示接口");
	}

	@Before
	public void caseUp() {
		hotBean=new HotBean();
		hotCall=new HotCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(hotCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Hot_001() {
		Logger.start(true,"接口返回状态");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			hotBean.setModule("live_hot_suggest");
			hotCall.setData(hotBean);
			hotCall.callService();		
			
			Logger.comment("hotCall send:" + hotCall.getEntityString());
			Logger.comment("hotCall response:" + hotCall.getReturnData());
			Logger.verifyEquals("OK", hotCall.getStatus(), "接口返回状态正确");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Hot_002() {
		Logger.start(true,"返回显示数量,总数大于10的情况下显示10条记录");

		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			hotBean.setModule("live_hot_suggest");
			hotCall.setData(hotBean);
			hotCall.callService();		
			//总数大于10的情况下显示10条记录
			
			//获取接口total值
			String totals = hotCall.getTotal();
			
			//获取接口返回记录数
			JSONArray list = hotCall.getHotInfo();
			Logger.debug(list);
			int listSize = list.length();
			
			Logger.comment("hotCall send:" + hotCall.getEntityString());
			Logger.comment("hotCall response:" + hotCall.getReturnData());
			//比较两个值
			
			if(Integer.parseInt(totals) >listSize && Integer.parseInt(totals)>=10){
				Logger.verifyEquals(10, listSize, "返回记录数正确");
			}else if(10>Integer.parseInt(totals) && Integer.parseInt(totals) >listSize){
				Logger.verifyEquals(listSize, listSize, "返回记录数正确");
			}else{
				Logger.verifyEquals(10, listSize, "返回记录数不正确");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Hot_003() {

		Logger.start(true,"热搜词显示顺序,按搜索次数从大到小");

		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			hotBean.setModule("live_hot_suggest");
			hotCall.setData(hotBean);
			hotCall.callService();		
			//按搜索次数从大到小
			
			//获取接口返回记录
			
			JSONArray hotInfo = hotCall.getHotInfo();
			int hotSize = hotInfo.length();
			JSONObject[] hotAry = new JSONObject[hotSize];
			//专门存放searchnum数组
			Integer[] numAry = new Integer[hotSize];
			//定义一个专门存放keyword数组
			String[] kwAry = new String[hotSize];
			//Logger.debug(hotInfo.get(0));
			for(int i=0;i<hotSize;i++){
				hotAry[i] = (JSONObject) hotInfo.get(i);
				numAry[i] = (Integer) hotAry[i].get("searchnum");
				kwAry[i] =  (String) hotAry[i].get("keyword");
					
			}
			int checks=0;
			for(int y=0;y<hotSize-1;y++){
				
				if(numAry[y] < numAry[y+1]){
					checks =1;
					Logger.verifyEquals("热搜词"+kwAry[y]+"在前", "热搜词"+kwAry[y+1]+"在前", "返回记录顺序不正确");
				}
			}
			if(checks !=1){
				Logger.verifyEquals("1", "1", "返回记录顺序正确");
			}
			
			Logger.comment("hotCall send:" + hotCall.getEntityString());
			Logger.comment("hotCall response:" + hotCall.getReturnData());
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}